package cn.stronglink.assetmanage.business.search.model;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.netrequest.business.patrol.PatrolService;
import cn.stronglink.netrequest.business.search.SearchService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 检索查找数据库操作
 * Created by zhengjinglan on 2018/6/19.
 */

public class SearchModel {
    private SearchService searchService;
    private onSearchDataListener listener;
    Gson gson = new Gson();


    public onSearchDataListener getSearchDataListener() {
        return listener;
    }

    public void setSearchDataListener(onSearchDataListener listener) {
        this.listener = listener;
    }

    public SearchModel() {
        searchService = new SearchService();
    }

    public void getSearchData() {
        try {
            searchService.downloadAssetData(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (listener != null) {
                        listener.onFailer(call, e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (listener != null) {
                        listener.onResponse(call, response);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteAllCheckData() {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().deleteAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().deleteAll();
    }

    public void saveSearchData(String requestJson) {
        Result<List<SearchAsset>> searchResult = null;
        searchResult = gson.fromJson(requestJson, new TypeToken<Result<List<SearchAsset>>>() {
        }.getType());
        if (searchResult != null && searchResult.getHttpCode() == HttpURLConnection.HTTP_OK) {
            if (searchResult.getData() != null) {
                List<SearchAsset> checkList = searchResult.getData();
                // 验收单列表
                if (checkList != null && checkList.size() > 0) {
                    //从服务器获取数据，成功后，清除本地入库验收数据库表
                    deleteAllCheckData();
                    for (SearchAsset searchAsset : checkList) {
                        searchAsset.setStatus(Constants.STATUS_ONE);
                        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().insert(searchAsset);
                        //将检索单列表保存在任务表中
                        Task task = new Task();
                        task.setId(searchAsset.getId());
                        task.setId_(searchAsset.getId_());
                        task.setStatus(searchAsset.getStatus());
//                        task.setBusinessType(Constants.STATUS_ONE);
                        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTaskDao().insertOrReplace(task);
                    }
                }
            }
        }
    }


    /**
     * 获取全部数据
     *
     * @return
     */
    public List<SearchAsset> getAllData() {
        List<SearchAsset> searchAssets = DaoManager
                .getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().queryBuilder().list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (searchAssets != null) {
            return searchAssets;
        }
        return null;
    }

    /**
     * 根据设备名称获得数据
     *
     * @param brandName 设备名称
     * @return
     */
    public List<SearchAsset> getDataByBrandName(String brandName) {
        List<SearchAsset> searchAssets = DaoManager
                .getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().queryBuilder()
                .where(SearchAssetDao.Properties.BrandName.eq(brandName)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (searchAssets != null) {
            return searchAssets;
        }
        return null;
    }

    /**
     * 根据RFID获得数据
     *
     * @param rfid 设备名称
     * @return
     */
    public List<SearchAsset> getDataByRfid(String rfid) {
        List<SearchAsset> searchAssets = DaoManager
                .getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().queryBuilder()
                .where(SearchAssetDao.Properties.RfidNo.eq(rfid)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (searchAssets != null) {
            return searchAssets;
        }
        return null;
    }

    /**
     * 根据资产编号获得数据
     *
     * @param sn 设备名称
     * @return
     */
    public List<SearchAsset> getDataBySn(String sn) {
        List<SearchAsset> searchAssets = DaoManager
                .getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().queryBuilder()
                .where(SearchAssetDao.Properties.SerialNo.eq(sn)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (searchAssets != null) {
            return searchAssets;
        }
        return null;
    }


    public interface onSearchDataListener {
        void onFailer(Call call, IOException ex);

        void onResponse(Call call, Response response);
    }
}
